//
//  DYSameContactCell.m
//  PrivatePhoto
//
//  Created by PrivatePhoto Team on 2021.
//  Copyright © 2021年 PrivatePhoto Team. All rights reserved.3.
//

#import "DYSameContactCell.h"
#import "DYCommonSCell.h"

@interface CASameContactHeaderView : UIView
@property (nonatomic, strong) UILabel *topLabel;
@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *phoneNumLabel;
@property (nonatomic, strong) UILabel *tipLabel;
@property (nonatomic, strong) UIButton *seleteBtn;
@property (nonatomic, copy) void (^selecteCompleteBlock)(BOOL seleted);
@end

@implementation CASameContactHeaderView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithHexString:@"#E8F1FF"];
        [self addSubViews];
    }
    return self;
}

- (void)layoutSubviews{
    [super layoutSubviews];
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.top.mas_offset(15);
    }];
    
    [self.contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.top.mas_equalTo(self.topLabel.mas_bottom).mas_offset(10);
        make.right.mas_offset(-10);
        make.height.mas_equalTo(60);
    }];
    
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.top.mas_offset(10);
        make.right.mas_offset(-10);
    }];
    [self.phoneNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.top.mas_equalTo(self.nameLabel.mas_bottom);
        make.right.mas_offset(-10);
        make.bottom.mas_offset(-6);
    }];
    
    [self.tipLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(10);
        make.top.mas_equalTo(self.contentView.mas_bottom).mas_offset(15);
    }];
    
    [self.seleteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_offset(-10);
        make.centerY.mas_equalTo(self.tipLabel.mas_centerY);
    }];
    
    
}

- (void)addSubViews{
    [self addSubview:self.topLabel];
    [self addSubview:self.contentView];
    [self.contentView addSubview:self.nameLabel];
    [self.contentView addSubview:self.phoneNumLabel];
    [self addSubview:self.tipLabel];
    [self addSubview:self.seleteBtn];
}

- (void)btnDidClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    !self.selecteCompleteBlock ?:self.selecteCompleteBlock(sender.selected);
    
}

#pragma mark -- setter/getter

- (UILabel*)topLabel{
    if (!_topLabel) {
        _topLabel = [[UILabel alloc] init];
        _topLabel.font = [UIFont boldSystemFontOfSize:15];
        _topLabel.textColor = [UIColor blackColor];
        _topLabel.text = @"检测出已重复的联系人";
    }
    return _topLabel;
}

- (UIView *)contentView{
    if (!_contentView) {
        _contentView = [[UIView alloc] init];
        _contentView.backgroundColor = [UIColor colorWithHexString:@"#DAE7FE"];
        _contentView.layer.masksToBounds = YES;
        _contentView.layer.cornerRadius = 12.0;
    }
    return _contentView;
}

- (UILabel*)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc] init];
        _nameLabel.font = [UIFont boldSystemFontOfSize:17];
        _nameLabel.textColor = [UIColor blackColor];
        _nameLabel.text = @"阿昌";
    }
    return _nameLabel;
}

- (UILabel*)phoneNumLabel{
    if (!_phoneNumLabel) {
        _phoneNumLabel = [[UILabel alloc] init];
        _phoneNumLabel.font = [UIFont boldSystemFontOfSize:15];
        _phoneNumLabel.textColor = [UIColor blackColor];
        _phoneNumLabel.text = @"13479902378";
    }
    return _phoneNumLabel;
}


- (UILabel*)tipLabel{
    if (!_tipLabel) {
        _tipLabel = [[UILabel alloc] init];
        _tipLabel.font = [UIFont boldSystemFontOfSize:15];
        _tipLabel.textColor = [UIColor blackColor];
        _tipLabel.text = @"选择要删除的联系人";
    }
    return _tipLabel;
}

- (UIButton *)seleteBtn{
    if (!_seleteBtn) {
        _seleteBtn = [[UIButton alloc] init];
        _seleteBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_seleteBtn setTitleColor:[UIColor colorWithHexString:@"#5C93FF"] forState:UIControlStateNormal];
        [_seleteBtn setTitle:@"选择全部" forState:UIControlStateNormal];
        [_seleteBtn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _seleteBtn;
}

@end

@interface DYSameContactCell ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, weak) CASameContactHeaderView *headerView;

@end

@implementation DYSameContactCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = [UIColor colorWithHexString:@"#E8F1FF"];
        [self configSubView];
        [self bindViewModel];
    }
    return self;
}

-(void)setFrame:(CGRect)frame
{
    frame.origin.x = 10;//这里间距为10，可以根据自己的情况调整
    frame.size.width -= 2 * frame.origin.x;
    frame.size.height -= 2 * frame.origin.x;
    [super setFrame:frame];
}

- (void)layoutSubviews{
    [super layoutSubviews];
    self.layer.cornerRadius = 12.f;
    self.layer.masksToBounds = YES;
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
}

- (void)configSubView{
    self.tableView.layer.cornerRadius = 12.f;
    self.tableView.layer.masksToBounds = YES;
    self.tableView.backgroundColor = [UIColor colorWithHexString:@"#E8F1FF"];
    [self.contentView addSubview:self.tableView];
    CGRect frame = CGRectMake(0, 0, self.width, 160);
    CASameContactHeaderView *headerView = [[CASameContactHeaderView alloc] initWithFrame:frame];
    @weakify(self)
    headerView.selecteCompleteBlock = ^(BOOL seleted) {
        @strongify(self)
        if (seleted) {
            [self.cellModel.seletedArray removeAllObjects];
            [self.cellModel.seletedArray addObjectsFromArray:self.cellModel.dataArray];
           
        }else{
            [self.cellModel.seletedArray removeAllObjects];
          
        }
        self.cellModel.seletedCount = self.cellModel.seletedArray.count;
        !self.headSeletedBlock ?: self.headSeletedBlock(self.cellModel.dataArray, seleted);
        [self.tableView reloadData];
    };
    self.tableView.tableHeaderView = headerView;
    self.headerView = headerView;
    
}

- (void)bindViewModel{
    @weakify(self)
    [RACObserve(self, cellModel.seletedCount) subscribeNext:^(id  _Nullable x) {
        @strongify(self)
        BOOL isSeletedAll = self.cellModel.seletedCount >= self.cellModel.dataArray.count;
        NSString *title = !isSeletedAll ? @"选择全部":@"取消全选";
        [self.headerView.seleteBtn setTitle:title forState:UIControlStateNormal];
    }];
}

#pragma make --- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.cellModel.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cell_Id = @"DYCommonSCell";
    DYCommonSCell *cell = [tableView dequeueReusableCellWithIdentifier:cell_Id];
    if (cell == nil) {
        cell = [[DYCommonSCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cell_Id];
        cell.contentView.backgroundColor = [UIColor whiteColor];
    }
    
    YXContactsModel *model = self.cellModel.dataArray[indexPath.row];
    [cell bindWithModel:model seletedArray:self.cellModel.seletedArray];
    @weakify(self)
    cell.seletedBtnBlock = ^(YXContactsModel *item, BOOL isSelected) {
        @strongify(self)
        if ([self.cellModel.seletedArray containsObject:item]) {
            if (!isSelected) {[self.cellModel.seletedArray removeObject:item];}
        }else{
            if (isSelected) {[self.cellModel.seletedArray addObject:item];}
        }
        self.cellModel.seletedCount = self.cellModel.seletedArray.count;
        !self.seletCompleteBlock ?: self.seletCompleteBlock(item, isSelected);
    };

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (!self.cellModel.dataArray.count) {return 0.01;}
    return 80;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YXContactsModel *contact = self.cellModel.dataArray[indexPath.row];
    !self.skipDetailBlock ?:self.skipDetailBlock(contact);
}

- (void)setDataArr:(NSArray *)dataArr{
    _dataArr = dataArr;
    YXContactsModel *contact = dataArr.firstObject;
    self.headerView.nameLabel.text = contact.name;
    self.headerView.phoneNumLabel.text = contact.mobile;
    self.cellModel.dataArray = dataArr;
    [self.tableView reloadData];
}

- (void)setCellModel:(CASameContactCellModel *)cellModel{
    _cellModel = cellModel;
    YXContactsModel *contact = cellModel.dataArray.firstObject;
    self.headerView.nameLabel.text = contact.name;
    self.headerView.phoneNumLabel.text = contact.mobile;
    _cellModel.seletedCount = cellModel.seletedArray.count;
    self.headerView.seleteBtn.selected = cellModel.seletedArray.count == cellModel.dataArray.count;
    [self.tableView reloadData];
}

- (void)bindWithModel:(CASameContactCellModel *)item seletedArray:(NSMutableArray *)seletedArray{
    [item.seletedArray removeAllObjects];
    item.seletedCount = 0;
    BOOL isSeleted = [seletedArray containsObject:item];
    if (isSeleted){
         [item.seletedArray addObjectsFromArray:item.dataArray];
         item.seletedCount = item.seletedArray.count;
    }
    self.cellModel = item;
}

- (void)bindModel:(CASameContactCellModel *)item{
    
//    BOOL isSeleted = [seletedArray containsObject:item];
//    if (isSeleted){
//         [item.seletedArray addObjectsFromArray:item.dataArray];
//         item.seletedCount = item.seletedArray.count;
//    }
    self.cellModel = item;
}

#pragma mark ---setter/getter
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.bounds style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.rowHeight = 60;
        _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
        _tableView.delaysContentTouches = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
      
        if (@available(iOS 11.0, *)) {
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            // Fallback on earlier versions
        }
    }
    
    return _tableView;
}

@end


@implementation CASameContactCellModel

- (instancetype)init{
    if (self = [super init]) {
        self.seletedArray = [[NSMutableArray alloc] init];
    }
    return self;
}

@end
